package mylibrary.io.nio.nonBlock.sample1;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Client {

	private ByteBuffer buff = ByteBuffer.allocate(512);
	
	public void query(String host, int port) throws Exception {
		
		InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), port);
		
		SocketChannel socket = null;
		byte[] bytes = new byte[512];
		
		while(true) {
			try {
				
				System.in.read(bytes);
				
				socket = SocketChannel.open();
				
				socket.connect(address);
				
				buff.clear();
				
				buff.put(bytes);
				buff.flip();
				
				socket.write(buff);
				
				buff.clear();
				
			}catch (Exception e) {
				e.printStackTrace();
			} finally {
				socket.close();
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		
		new Client().query("localhost", 8099);
	}
}
